package com.dxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxp.common.CustomException;
import com.dxp.dto.DishDto;
import com.dxp.mapper.DishMapper;
import com.dxp.pojo.*;
import com.dxp.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    //新增菜品，同时添加菜品信息,操作两张表
    @Autowired
    DishFlavorService dishFlavorService;
    @Autowired
    DishService dishService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealService setmealService;
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        /*flavors=flavors.stream().map(item ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());*/
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }
    @Override
    public Page listDish(int page, int pageSize, String name) {
        Page<Dish> dishInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoInfo  = new Page<>();
        QueryWrapper<Dish> dishWrapper = new QueryWrapper<Dish>();
        dishWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        dishWrapper.orderByDesc("update_time");
        dishService.page(dishInfo,dishWrapper);
        BeanUtils.copyProperties(dishInfo,dishDtoInfo,"records");
        List<Dish> dishInfoRecords = dishInfo.getRecords();
        List<DishDto> list = new ArrayList<DishDto>();
        for (Dish dishInfoRecord : dishInfoRecords) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dishInfoRecord,dishDto);
            Long categoryId = dishInfoRecord.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            list.add(dishDto);
        }
        //stream流方式
        /*List<DishDto> list=dishInfoRecords.stream().map((item) ->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());*/
        dishDtoInfo.setRecords(list);
        return dishDtoInfo;
    }
    @Override
    public DishDto getByIdUpdateDish(Long id) {
        Dish dish = dishService.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
        wrapper.eq("dish_id",dish.getId());
        List<DishFlavor> flavorList = dishFlavorService.list(wrapper);
        dishDto.setFlavors(flavorList);
        return dishDto;
    }
    @Override
    public void updateDish(DishDto dishDto) {
        //先修改基本的信息
        this.updateById(dishDto);
        //修改口味:可以先删除 再重新添加
        QueryWrapper<DishFlavor> flavorQueryWrapper = new QueryWrapper<>();
        flavorQueryWrapper.eq("dish_id",dishDto.getId());
        dishFlavorService.remove(flavorQueryWrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void deleteWithDish(List<Long> ids) {
        //先判断是否在售
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.in("id",ids);
        dishQueryWrapper.eq("status",1);
        int count = dishService.count(dishQueryWrapper);
        //在售的话抛异常
        if(count>0)
        {
            throw new CustomException("存在菜品正在售卖,无法删除");
        }
        //停售的话开始删除,先删除菜品信息
        dishService.removeByIds(ids);
        //再删除口味表
        QueryWrapper<DishFlavor> dishFlavorQueryWrapper = new QueryWrapper<>();
        dishFlavorQueryWrapper.in("dish_id",ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);
        //还要把关联的套餐改为停售状态
        QueryWrapper<SetmealDish> setmealDishQueryWrapper = new QueryWrapper<>();
        setmealDishQueryWrapper.in("dish_id",ids);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishQueryWrapper);
        setmealDishList.stream().map((item)->{
            Long setmealId = item.getSetmealId();
            Setmeal setmeal = setmealService.getById(setmealId);
            setmeal.setStatus(0);
            setmealService.updateById(setmeal);
            return item;
        }).collect(Collectors.toList());
    }
}
